package com.mt.fpb.controller;

import com.alibaba.fastjson.JSONObject;
import com.mt.fpb.common.util.DateUtil;
import com.mt.fpb.mapper.TAirMonitorRecordMapper;
import com.mt.fpb.mapper.TMineInfoMapper;
import com.mt.fpb.mapper.TWarningMapper;
import com.mt.fpb.model.TAirMonitorRecord;
import com.mt.fpb.model.TWarning;
import com.mt.fpb.model.dto.BaseQueryParams;
import com.mt.fpb.model.vo.CommonResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 2021年6月12日17:07:48
 * 矿山安全生产系统  LT
 */
@RestController
@RequestMapping("/TwarningKuSan")
public class TwarningKuSanController {
    @Resource
    private TWarningMapper tWarningMapper;

    @Resource
    private TMineInfoMapper tMineInfoMapper;
    @Resource
    private TAirMonitorRecordMapper tAirMonitorRecordMapper;

    /**
     * 汇总信息 LT
     * 左上
     * 2021年6月12日17:16:56
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getSummarizing")
    public CommonResult getSummarizing(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        JSONObject json = new JSONObject();
        //矿山总计
        json.put("mineCount", tMineInfoMapper.selectAll().size());
        //今日运作矿产
        List<Integer> list = tMineInfoMapper.TodaySwork(DateUtil.front(), DateUtil.getCurrentDate("yyyy-MM-dd HH"), queryParams.getMineId());
        json.put("operationDays", list.size());
        //总计异常于违规
        example.createCriteria().orEqualTo("warningType", 1).
                orEqualTo("warningType", 2).
                orEqualTo("warningType", 6).
                orEqualTo("warningType", 7);
        json.put("abnormalViolations", tWarningMapper.selectByExample(example).size());
        example.clear();
        //今日异常违规
        example.createCriteria().orEqualTo("warningType", 1).
                orEqualTo("warningType", 2).
                orEqualTo("warningType", 6).
                orEqualTo("warningType", 7).andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%");
        json.put("abnormalViolationsDays", tWarningMapper.selectByExample(example).size());
        example.clear();
        //TSP超标总数
        example.createCriteria().andEqualTo("warningType", 1);
        json.put("TSPExceeding", tWarningMapper.selectByExample(example).size());
        example.clear();
        //今日TSP超标数
        example.createCriteria().andEqualTo("warningType", 1).andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%");
        json.put("TSPExceedingDays", tWarningMapper.selectByExample(example).size());
        example.clear();
        return CommonResult.success(json);
    }

    /**
     * 告警信息 LT
     * 左中
     * 2021年6月12日17:45:38
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getAlarmInfo")
    public CommonResult getAlarmInfo(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        example.createCriteria().orEqualTo("warningType", 1).
                orEqualTo("warningType", 2).
                andEqualTo("mineId",queryParams.getMineId());
        List<TWarning> tWarnings = tWarningMapper.selectByExample(example);
        return CommonResult.success(tWarnings);
    }

    /**
     * 未戴安全帽 LT
     * 右下
     * 2021年6月12日17:47:27
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getNotHelmet")
    public CommonResult getNotHelmet(BaseQueryParams queryParams) {
        //查询矿山未带安全帽记录
        List<Map> list = tWarningMapper.getNotHelmet(queryParams);
        return CommonResult.success(list);
    }

    /**
     * 环境监测 LT
     * 右上
     * 2021年6月12日18:00:45
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getEnvironmentalMonitoring")
    public CommonResult getEnvironmentalMonitoring(BaseQueryParams queryParams) {
        Example example = new Example(TAirMonitorRecord.class);
        example.createCriteria().
                andEqualTo("mineId", queryParams.getMineId());
        example.orderBy("updateTime").desc();
        example.selectProperties("tsp", "pm25");
        List<TAirMonitorRecord> tAirMonitorRecords = tAirMonitorRecordMapper.selectByExample(example);
        if (tAirMonitorRecords.size() == 0) {
            return CommonResult.success(tAirMonitorRecords);
        }
        return CommonResult.success(tAirMonitorRecords.get(0));
    }

    /**
     * 年度安全生产时长占比 LT
     * 左下
     * 2021年6月12日18:11:11
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getTheAnnualTime")
    public CommonResult getTheAnnualTime(BaseQueryParams queryParams) {
        //年度安全生产时长占比
        Integer count = tWarningMapper.getTheAnnualTime(DateUtil.getCurrentDate("yyyy"), queryParams.getMineId());
        //判断是否为空
        if (StringUtils.isEmpty(count)) {
            return CommonResult.success(0);
        }
        // 1减去违规数获取时长占比
        int result = (1 - count) * 100;
        return CommonResult.success(result);
    }

}
